<?php

namespace app\admin\controller;

use think\Cache;
use think\Controller;
use think\Db;
use think\Exception;
use think\Request;
use think\Session;

class Base extends Controller
{
    public $menus;
    public $menus_tree;
    public $account;
    public $model;
    public $merchant_menus_tree;
    public function _initialize()
    {
        if(!$this->is_login()){
            $this->redirect(url('admin/login/index'));
        }
        $this->menus();
        $this->merchant_menus();
        $this->check_menus();
        $this->assign([
            'admin_edit'=>config('identification.admin_edit'),
            'edit_status' => config('identification.admin_edit_status'),
            'admin_add' => config('identification.admin_add'),
            'admin_edit_menus' => config('identification.admin_edit_menus'),
            'menu_add' => config('identification.menu_add'),
            'menu_edit_sort' => config('identification.menu_edit_sort'),
            'menu_edit_show' => config('identification.menu_edit_show'),
            'group_add' => config('identification.group_add'),
            'group_edit' => config('identification.group_edit'),

        ]);
    }

    /**
     * 查询后台菜单
     *
     * @return \think\Response
     */
    public function menus()
    {
        if($redis = Cache::store('redis')->get(config('redis.xm_menus_all'))){
            $this->menus_tree = json_decode($redis,true);
        }else{
            try{
                $menus = model('menu')->where(['status'=>config('code.status_normal')])->order('sort','desc')->select();
                $this->menus_tree = $this->get_menus_tree($menus);
            }catch (\Exception $e){
                $this->error($e->getMessage());
            }
        }
    }

    /**
     * 将菜单进行父子排序.
     *
     * @return \think\Response
     */
    public function get_menus_tree($data,$fid = 0,$level = 0)
    {
        static $res = [];
        foreach($data as $k => $v){
            if($v['fid'] == $fid){
                $v['level'] = $level;
                $res[$v['id']] = $v;
                $this->get_menus_tree($data,$v['id'],$level +1);
            }
        }
        Cache::store('redis')->set(config('redis.xm_menus_all'),json_encode($res));
        return $res;
    }

    /**
     * 获取redis中的用户数据
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function get_redis_info()
    {
        if(!$this->account){
            return $this->account = Session::get(config('user.user_data'));
        }
        return $this->account;
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function is_login()
    {
        $user = $this->get_redis_info();
        if($user){
            if(time() - Session::get(config('user.last_time')) > 60*10){
                Session::delete('last_time');
                return false;
            }else{
                Session::set(config('user.last_time'),time());
                return true;
            }
        }else{
            return false;
        }
    }

    /**
     * 修改状态值
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function edit_status()
    {
        $data = input('get.');
        if(empty($data['id'])){
           $this->error('id不正确');
        }
        $this->model = \request()->controller();
        $res = model($this->model)->save(['status'=>$data['status']],['id'=>$data['id']]);
        if (!$res) {
            $this->redirect($_SERVER['HTTP_REFERER']);
        } else {
            $this->redirect($_SERVER['HTTP_REFERER']);
        }
    }

    /**
     * 权限验证
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function check_menus()
    {
        $menus = [];
        //超级管理员跳过权限
        if($this->account->level != 1){
            $account = Db::table('xm_menu')->where(['id'=>['in',$this->account['menus']]])->select();;
            //根据用户信息查询权限
            foreach($account as $k => $v){
                $menus[] = $v['identification'];
             }
             //获取权限树
            $menus[] = 'Menu/tree_data';
            //选择权限
            $menus[] = 'Admin/admin_menus';
            $data = array_filter($menus);
            if(!in_array(\request()->controller().'/'.\request()->action(),$menus)){
                exit('您没有访问权限');
            }
            $tree = [];
            $treeData = [];
            foreach($account as $v){
                $treeData[$v['id']] = $v;
            }
            foreach($treeData as $k => $v){
                if($v['fid'] == 0){
                    $tree[] = &$treeData[$k];
                }else{
                    $treeData[$v['fid']]['child'][] = &$treeData[$k];
                }
            }
            $this->assign('identification',$data);
            $this->assign('menus',$tree);
        }else{
            $account = Db::table('xm_menu')->where(['status'=>1])->select();
            $tree = [];
            $treeData = [];
            foreach($account as $k => $v){
                $menus[] = $v['identification'];
            }
            foreach($account as $v){
                $treeData[$v['id']] = $v;
            }
            foreach($treeData as $k => $v){
                if($v['fid'] == 0){
                    $tree[] = &$treeData[$k];
                }else{
                    $treeData[$v['fid']]['child'][] = &$treeData[$k];
                }
            }
            $this->assign('identification',$menus);
            $this->assign('menus',$tree);
        }
    }
    //排序商户权限
    public function merchant_menus()
    {
        if($redis = Cache::store('redis')->get(config('redis.xm_merchant_menus_all'))){
            $this->merchant_menus_tree = json_decode($redis,true);
        }else{
                $menus = model('merchant_menu')->where(['status'=>config('code.status_normal')])->order('sort','desc')->select();
                $this->merchant_menus_tree = $this->get_merchant_menus_tree($menus);
        }
    }
    public function get_merchant_menus_tree($data,$fid = 0,$level = 0)
    {
        static $res = [];
        foreach($data as $k => $v){
            if($v['fid'] == $fid){
                $v['level'] = $level;
                $res[$v['id']] = $v;
                $this->get_merchant_menus_tree($data,$v['id'],$level +1);
            }
        }
        Cache::store('redis')->set(config('redis.xm_merchant_menus_all'),json_encode($res));
        return $res;
    }
}
